<?php

namespace app\adminmall\controller;
use app\adminmall\model\WechatModel;
use app\common\controller\AdminBaseController;
use think\Db;
/**
 * 商家公众号
 */
class WechatController extends AdminBaseController
{
 	 protected $model;
	
    protected function _initialize() {
        parent::_initialize();
        $this->model = new WechatModel();
    }
	
     /**
     * 商户公众号列表
     * @author [longx] 
     */
    public function index($page = 1){
 		$map=[];
 		if(request()->isPost()){
		 	$param = input('post.');         
			//公众号名
			if($param['publicname']){
				$map['a.publicname'] = ['like', '%'.$param['publicname'].'%'];
			}
			//商家名称
			if($param['store_name']){
				$map['b.store_name'] = ['like', '%'.$param['store_name'].'%'];
			}
			//状态
			if($param['status']!=2){
				$map['a.status'] = ['like', '%'.$param['status'].'%'];
			}	
		 	//创建时间
			if($param['add_time']&&$param['end_time']){
				$map['a.add_time'] = ['between time', [$param['add_time'],$param['end_time']]];
			}
		}				    					
	    $list=Db::name('mall_wechatset')
	 	->alias('a')
 		->join('mall_agent b','a.agent_id=b.id')
 		->field('a.*,b.store_name')
	   	->where($map)
	   	->paginate(10);
	    return $this->fetch('index', ['list' => $list]);
	}

    /**
     * 
     *添加
     * @author [longx] 
     */
    public function add()
    {
        if(request()->isAjax()){
        	$param=input('post.');
        	$param['status']=input('status') ? true : false;
        	$param['pcbindstatus']=input('pcbindstatus') ? true : false;
        	if($param['status']){
        		$param['status']='1';
        	}else{
        		$param['status']='0';
        	}
        	if($param['pcbindstatus']){
        		$param['picbindstatus']='1';
        	}else{
        		$param['picbindstatus']='0';
        	}

			$param['createtime'] = now_time();
			if ($this->model->allowField(true)->save($param) !== false) {
                    $this->success('添加成功', url('Wechat/index'));
            } else {
                $this->error('添加失败');
            }
        }
        return $this->fetch();
    }


    /**
     * [edit 编辑]
     * @return [type] [description]
     * @author [longx]
     */
    public function edit($id)
    {     $id = input('param.id');
        if(request()->isAjax()){
            $param = input('post.');       
            $param['status']=input('status') ? true : false;
        	$param['pcbindstatus']=input('pcbindstatus') ? true : false;
        	if($param['status']){
        		$param['status']='1';
        	}else{
        		$param['status']='0';
        	}
        	if($param['pcbindstatus']){
        		$param['pcbindstatus']='1';
        	}else{
        		$param['pcbindstatus']='0';
        	}
			$param['createtime'] = now_time();
			if ($this->model->allowField(true)->save($param,$id) !== false) {
                    $this->success('修改成功', url('Wechat/index'));
            } else {
                $this->error('修改失败');
            }  
         	
        }

        $this->assign('ad',$this->model->find($id));
        return $this->fetch();
    }



    /**
     * [del删除]
     * @author [longx] 
     */
    public function delete()
    {
        $id = input('param.id');
        if ($this->model->destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
}